package net.shopxx.action.shop;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.bean.Pager;
import net.shopxx.entity.Article;
import net.shopxx.entity.ArticleCategory;
import net.shopxx.entity.DeliveryType;
import net.shopxx.entity.OrderPointer;
import net.shopxx.entity.PaymentConfig;
import net.shopxx.entity.Pointproduct;
import net.shopxx.entity.ProductConsult;
import net.shopxx.entity.Receiver;
import net.shopxx.entity.Order.OrderStatus;
import net.shopxx.entity.Order.ShippingStatus;
import net.shopxx.entity.Product.WeightUnit;
import net.shopxx.service.AreaService;
import net.shopxx.service.ArticleCategoryService;
import net.shopxx.service.ArticleService;
import net.shopxx.service.DeliveryTypeService;
import net.shopxx.service.OrderPointerService;
import net.shopxx.service.PaymentConfigService;
import net.shopxx.service.PointproductService;
import net.shopxx.service.ReceiverService;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.util.StringUtil;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 积分商品
 */

@ParentPackage("shop")
public class PointproductAction extends BaseShopAction {

	private static final long serialVersionUID = -4433964283757192334L;

	private static final String acId= "402881e53a58bea4013a5905de07000c";
	@Resource
	private ArticleService articleService;
	@Resource
	private ArticleCategoryService articleCategoryService;
	@Resource
	private PointproductService pointproductService;
	@Resource
	private DeliveryTypeService deliveryTypeService;
	@Resource
	private PaymentConfigService paymentConfigService;
	@Resource
	private OrderPointerService orderPointerService;
	@Resource
	private ReceiverService receiverService;
	@Resource
	private AreaService areaService;
	
	private Pointproduct pointproduct;

	private String imageUrlFileName;
	
	private String maxValue;
	private String minValue;
	private String kindType;
	private Receiver receiver;// 其它收货地址
	private DeliveryType deliveryType;// 配送方式
	private Boolean isSaveReceiver;// 是否保存收货地址
	private String memo;// 附言
	private List<Article> articleList;
	private List<Pointproduct> weekTopList;
	private OrderPointer order;
	

	// 编辑
	public String view() {
		pointproduct = pointproductService.load(id);
		return VIEW;
	}
	// 订单保存
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "deliveryType.id", message = "请选择配送方式！")
		}
	)
	@InputConfig(resultName = "error")
	public String save()
	{
		if (StringUtils.isNotEmpty(receiver.getId())) {
			receiver = receiverService.load(receiver.getId());
			if (areaService.getAreaString(receiver.getAreaPath()) == null) {
				addActionError("收货地址信息不完整，请补充收货地址信息！");
				redirectionUrl = "receiver!edit.action?id=" + receiver.getId();
				return ERROR;
			}
		} else {
			if (StringUtils.isEmpty(receiver.getName())) {
				addActionError("收货人不允许为空！");
				return ERROR;
			}
			if (StringUtils.isEmpty(receiver.getAreaPath())) {
				addActionError("地区不允许为空！");
				return ERROR;
			}
			if (StringUtils.isEmpty(receiver.getAddress())) {
				addActionError("联系地址不允许为空！");
				return ERROR;
			}
			if (StringUtils.isEmpty(receiver.getZipCode())) {
				addActionError("邮编不允许为空！");
				return ERROR;
			}
			if (StringUtils.isEmpty(receiver.getPhone()) && StringUtils.isEmpty(receiver.getMobile())) {
				addActionError("联系电话、联系手机必须填写其中一项！");
				return ERROR;
			}
			if (!areaService.isAreaPath(receiver.getAreaPath())) {
				addActionError("地区错误！");
				return ERROR;
			}
			if (isSaveReceiver == null) {
				addActionError("是否保存不允许为空！");
				return ERROR;
			}
			if (isSaveReceiver) {
				receiver.setIsDefault(false);
				receiver.setMember(getLoginMember());
				receiverService.save(receiver);
			}
		}
		
		pointproduct = pointproductService.load(id);
		deliveryType = deliveryTypeService.load(deliveryType.getId());
		OrderPointer order = new OrderPointer();
//		order.setId(UUID.randomUUID().toString());
		order.setPointproduct(pointproduct);
		order.setMember(getLoginMember());
		order.setOrderSn(UUID.randomUUID().toString());
		
		order.setOrderStatus(OrderStatus.unprocessed);
		order.setShippingStatus(ShippingStatus.unshipped);
		order.setDeliveryTypeName(deliveryType.getName());
		order.setProductTotalPrice(new BigDecimal("0"));
		order.setTotalAmount(new BigDecimal("0"));
		order.setPaidAmount(new BigDecimal("0"));
		order.setProductTotalQuantity(1);
		order.setProductWeight(new Double(0));
		order.setProductWeightUnit(WeightUnit.g);
		order.setShipName(receiver.getName());
		order.setShipArea(areaService.getAreaString(receiver.getAreaPath()));
		order.setShipAreaPath(receiver.getAreaPath());
		order.setShipAddress(receiver.getAddress());
		order.setDeliveryTypeName("noName");
		order.setShipZipCode(receiver.getZipCode());
		order.setShipPhone(receiver.getPhone());
		order.setShipMobile(receiver.getMobile());
		order.setMemo(memo);
		order.setMember(getLoginMember());
		order.setDeliveryType(deliveryType);
		orderPointerService.save(order);
		return "save";
	}
	
	public String exchange()
	{
		pointproduct = pointproductService.load(id);
		return "exchange";
	}
	
	// 获取所有配送方式
	public List<DeliveryType> getAllDeliveryType() {
		return deliveryTypeService.getAll();
	}
	
	// 获取所有支付方式
	public List<PaymentConfig> getAllPaymentConfig() {
		return paymentConfigService.getAll();
	}

	// 列表
	public String list() {
		if (pager == null) {
			pager = new Pager();
		}
		pager.setPageSize(ProductConsult.MAX_CONSULT_COUNT);
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Pointproduct.class);
		if(!StringUtil.isEmpty(kindType))
		{
//			detachedCriteria.add(Restrictions.like("type",Integer.parseInt(kindType)));
		}
		if(!StringUtil.isEmpty(minValue))
		{
			detachedCriteria.add(Restrictions.ge("point", Integer.parseInt(minValue)));
		}
		if(!StringUtil.isEmpty(maxValue))
		{
			detachedCriteria.add(Restrictions.le("point", Integer.parseInt(maxValue)));
		}
		pager = pointproductService.findByPager(pager, detachedCriteria);
		
		//兑换排行榜
		
		//公告列表
		ArticleCategory articleCategory = articleCategoryService.load(acId);
		articleList = articleService.getArticleList(articleCategory,0,10);
		
		return LIST;
	}

	public Pointproduct getPointproduct() {
		return pointproduct;
	}

	public void setPointproduct(Pointproduct pointproduct) {
		this.pointproduct = pointproduct;
	}

	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(
				getRequest().getSession().getServletContext()).getCache(
				getRequest(), PageContext.APPLICATION_SCOPE);
		cache.flushAll(new Date());
	}

	public String getImageUrlFileName() {
		return imageUrlFileName;
	}

	public void setImageUrlFileName(String imageUrlFileName) {
		this.imageUrlFileName = imageUrlFileName;
	}

	public String getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(String maxValue) {
		this.maxValue = maxValue;
	}

	public String getMinValue() {
		return minValue;
	}

	public void setMinValue(String minValue) {
		this.minValue = minValue;
	}

	public String getKindType() {
		return kindType;
	}

	public void setKindType(String kindType) {
		this.kindType = kindType;
	}
	public Receiver getReceiver() {
		return receiver;
	}
	public void setReceiver(Receiver receiver) {
		this.receiver = receiver;
	}
	public DeliveryType getDeliveryType() {
		return deliveryType;
	}
	public void setDeliveryType(DeliveryType deliveryType) {
		this.deliveryType = deliveryType;
	}
	public Boolean getIsSaveReceiver() {
		return isSaveReceiver;
	}
	public void setIsSaveReceiver(Boolean isSaveReceiver) {
		this.isSaveReceiver = isSaveReceiver;
	}
	public String getMemo() {
		return memo;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public List<Article> getArticleList() {
		return articleList;
	}
	public void setArticleList(List<Article> articleList) {
		this.articleList = articleList;
	}
	public List<Pointproduct> getWeekTopList() {
		return weekTopList;
	}
	public void setWeekTopList(List<Pointproduct> weekTopList) {
		this.weekTopList = weekTopList;
	}
	public OrderPointer getOrder() {
		return order;
	}
	public void setOrder(OrderPointer order) {
		this.order = order;
	}
	
	

}